<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input
            clearable
            placeholder="商户ID"
            v-model="form.merchant_id"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            clearable
            placeholder="商户名称"
            v-model="form.merchant_name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            clearable
            placeholder="订单流水号"
            v-model="form.order_no"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核结果">
          <el-select clearable v-model="form.audit_status">
            <el-option
              v-for="item in auditStatus"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提现时间">
          <el-date-picker
            clearable
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column
            prop="audit_order_id"
            label="ID"
            align="center"
            width="60"
          />
          <el-table-column label="当前审核信息" align="left" width="200">
            <template #default="scope">
              <p>
                审核步骤：第 {{ scope.row.audit_level }} 审
                <el-button
                  type="primary"
                  size="small"
                  @click="handleOperate(scope.$index, scope.row, 'view')"
                  >查看</el-button
                >
              </p>
              <p>审 核 组：{{ scope.row.group_name }}</p>
              <p v-if="scope.row.audit_status == 1">
                审核人员：{{ scope.row.audit_user }}
              </p>
              <p>
                审核状态：<el-tag
                  :type="
                    scope.row.audit_status == 1
                      ? 'success'
                      : scope.row.audit_status == 2
                      ? 'danger'
                      : 'warning'
                  "
                >
                  {{ scope.row.audit_status_name }}
                </el-tag>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="提现时间"
            :formatter="formatterTime"
            align="center"
            width="85"
          ></el-table-column>
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="110"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
          />
          <el-table-column
            prop="order_no"
            label="订单流水号"
            align="center"
            width="150"
          />
          <el-table-column label="金额" align="center">
            <template #default="scope"
              >{{ scope.row.amount }} {{ scope.row.currency }}</template
            >
          </el-table-column>
          <el-table-column prop="fee" label="手续费" align="center" />
          <el-table-column label="付款信息" align="left" width="300">
            <template #default="scope">
              <div
                v-for="x in scope.row.sends"
                style="border-bottom: 1px solid #eeeeee"
              >
                <p>付款银行：{{ x.send_bank }} {{ x.send_branch }}</p>
                <p>付款账号：{{ x.send_card_number }}</p>
                <p>付款人：{{ x.send_card_name }}</p>
                <p>付款金额：{{ x.send_amount }} {{ x.send_currency }}</p>
                <p>付款时间：{{ formatDates(x.send_pay_time) }}</p>
                <p v-if="x.send_swift_code">Swift码：{{ x.send_swift_code }}</p>
                <p v-if="x.send_extended">补充信息：{{ x.send_extended }}</p>
                <p v-if="x.payment_way">支付方式：{{ x.payment_way }}</p>
                <p v-if="x.payment_order_no">
                  支付流水号：{{ x.payment_order_no }}
                </p>
                <p v-if="x.attachments">
                  <el-image
                    style="width: 50px; height: 50px"
                    v-for="i in x.attachments"
                    :src="i"
                    alt
                    fit="fill"
                    :preview-src-list="[i]"
                    :preview-teleported="true"
                  />
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="收款信息" align="left" width="300">
            <template #default="scope">
              <p>收款名称：{{ scope.row.receive_name }}</p>
              <p>
                收款银行：{{ scope.row.receive_bank }}
                {{ scope.row.receive_branch }}
              </p>
              <p>收款地址：{{ scope.row.receive_bank_address }}</p>
              <p>收款账号：{{ scope.row.receive_card_number }}</p>
              <p>收款姓名：{{ scope.row.receive_card_name }}</p>
              <p>Swift码：{{ scope.row.receive_swift_code }}</p>
              <p v-if="scope.row.receive_extended">
                补充信息：{{ scope.row.receive_extended }}
              </p>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="订单状态" align="center">
            <!--0:处理中;1:成功;2:失败 3:取消-->
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.status == 1
                    ? 'success'
                    : scope.row.status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.status == 1
                    ? "成功"
                    : scope.row.status == 2
                    ? "失败"
                    : scope.row.status == 3
                    ? "取消"
                    : "处理中"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="mer_review_status"
            label="商户审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.mer_review_status == 1
                    ? 'success'
                    : scope.row.mer_review_status == 2
                    ? 'danger'
                    : 'warning'
                "
              >
                {{
                  scope.row.mer_review_status == 1
                    ? "已审核"
                    : scope.row.mer_review_status == 2
                    ? "拒绝"
                    : "未审核"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="sys_review_status"
            label="系统审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.sys_review_status == 1
                    ? 'success'
                    : scope.row.sys_review_status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.sys_review_status == 1
                    ? "已审核"
                    : scope.row.sys_review_status == 2
                    ? "拒绝"
                    : "未审核"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sys_auditor" label="审核人" align="center" />
          <el-table-column
            prop="sys_audit_time"
            label="审核时间"
            :formatter="formatterTime"
            align="center"
            width="80"
          ></el-table-column>
          <el-table-column prop="description" label="备注" align="center" />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="200"
          >
            <template #default="scope">
              <el-button
                type="danger"
                v-if="
                  scope.row.mer_review_status === 1 &&
                  scope.row.sys_review_status === 0 &&
                  scope.row.allow_review === 1
                "
                @click="handleOperate(scope.$index, scope.row, 'audit')"
                >审核</el-button
              >
              <el-button
                type="primary"
                v-if="
                  scope.row.mer_review_status === 1 &&
                  scope.row.sys_review_status === 0
                "
                @click="handleOperate(scope.$index, scope.row, 'records')"
                >资产明细</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
      @handleUploaderImgRequest="handleUploader"
      :auditHistory="auditHistory"
      :btnType="btnType"
      :cardSelect="cardSelect"
    />
    <RecordForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      :recordHistory="recordHistory"
      :recordAssets="recordAssets"
      :dialogFormVisible="recordVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  formatterTime,
  pickerDefaultTime,
  timestamp,
  formatDates,
} from "@/utils/utils";
import {
  reviewWithdrawOrderApi,
  reviewWithdrawAuditApi,
  reviewHistoryApi,
  reviewCurrencyRecordsApi,
} from "@/api/ExAssistant/review";
import { sysBankCardSelectApi } from "@/api/ExAssistant/settings";
import DialogForm from "./components/auditForm.vue";
import RecordForm from "./components/recordForm.vue";
const loading = ref(false);
const tableData = ref([]);
const auditStatus = ref([]);
const auditHistory = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref("");
const btnLoading = ref(false);
const btnType = ref("");
const recordHistory = ref([]);
const recordAssets = ref({});
const recordVisible = ref(false);
const cardSelect = ref([]);
const formData = {
  merchant_id: "",
  merchant_name: "",
  order_no: "",
  audit_status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const dialogFormData = {
  audit_order_id: "",
  order_no: "",
  review_status: "",
  review_reason: "",
  send_bank: "",
  send_card_number: "",
  send_card_name: "",
  payment_way: "",
  payment_order_no: "",
  payment_image: "",
  payment_image_show: "",
  send_amount: "",
  sys_card_id: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    order_no: form.order_no,
    audit_status: form.audit_status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await reviewWithdrawOrderApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    auditStatus.value = res.data.extra.audit_status;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "audit") {
    btnType.value = "audit";
    dialogTitle.value = "订单审核";
    Object.assign(dialogForm, {
      //...row
      audit_order_id: row.audit_order_id,
      order_no: row.order_no,
      review_status: "",
      cus_audit_level: row.audit_level,
      currency: row.currency,
      amount: row.amount,
      settled_amount: row.settled_amount,
    });
    dialogVisible.value = true;
    const his = await reviewHistoryApi({ order_id: row.audit_order_id });
    auditHistory.value = his.data.results;
    if (row.audit_level == his.data.results.length) {
      const card = await sysBankCardSelectApi();
      cardSelect.value = card.data;
    }
  } else if (type === "view") {
    btnType.value = "view";
    dialogTitle.value = "查看审核记录";
    dialogVisible.value = true;
    const his = await reviewHistoryApi({ order_id: row.audit_order_id });
    auditHistory.value = his.data.results;
  } else if (type === "records") {
    dialogTitle.value = "资产变动明细";
    recordVisible.value = true;
    const his = await reviewCurrencyRecordsApi({
      audit_id: row.audit_order_id,
    });
    recordHistory.value = his.data.results;
    recordAssets.value = his.data.extra;
  }
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    let res;
    res = await reviewWithdrawAuditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  recordVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  recordVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
};
//上传图标
const handleUploader = (res) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    dialogForm.payment_image = res.data.name || "";
    dialogForm.payment_image_show = res.data.pic_url || "";
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
